<?php
require 'parts/_header.php';
require "controllers/tableDetailController.php";
?>
<script language="JavaScript" type="text/javascript" src="js/createFields.js"></script>
<!--Крошки-->
<div id="breadcrumbs"><a href="/admin/databaseList.php">Базы данных</a> > <a href="/admin/databaseDetail.php?dbName=<?= $dbName ?>"><?= $dbName ?></a> ><?= $dbTableName ?></div>

<table border="1" width="800">
    <caption><?= $table->name ?></caption>
    <tr>
        <?php foreach ($aFields as $aColumnProperties) : ?>
            <th><?= $aColumnProperties['Field'] ?>
    <form method="POST" style="display: inline">
        <input class="button-delete" type="submit" name="deleteField" value=""/>
        <input type="hidden" name="fieldName" value="<?= $aColumnProperties['Field']?>"/>
    </form>
                
            </th>
        <?php endforeach; ?>
        <th width="70"></th>
        <th width="70"></th>
    </tr>

    <?php foreach ($tableData as $aData) : ?>
        <form method="POST">

            <tr>
                <?php $keyValue = false; ?>
                <?php foreach ($aData as $key => $value) : ?>

                    <?php if ($key == $keyField)
                        $keyValue = $value; ?>    

                    <td><input type="text" name ="<?= $key ?>" value="<?= $value ?>" <?= ($key == $keyField) ? 'readonly' : '' ?>/></td>

                <?php endforeach; ?>
                <td>
                    <input class="button-delete" type="submit" name="delete" value="">
                </td>
                <td>
                    <input class="button-edit" type="submit" name="edit" value="">
                </td>
            </tr>
            <input type="hidden" name="dbTableName" value="<?= $dbTableName ?>"/>
            <input type="hidden" name="dbName" value="<?= $dbName ?>"/>
        </form>        
    <?php endforeach; ?>
</table>


<?php for ($i = 0; $i < $countPage; $i++) : ?>
    <a href="tableDetail.php?dbTableName=<?= $dbTableName ?>&dbName=<?= $dbName ?>&page=<?= $i ?>"><?= $i + 1 ?></a>

<?php endfor; ?>

<form action="tableEdit.php" method="POST">
    <input class="button" type="submit" name="editTable" value="Edit table"/>
    <input type="hidden" name="dbTableName" value="<?= $dbTableName ?>"/>
    <input type="hidden" name="dbName" value="<?= $dbName ?>"/>
</form>

<form action ="" method="POST">
    
    <input class="button" type="button" id="addField" value="ADD FIELD"/>
    <input class="button" type="submit" id="submitField" name="createField" value="CREATE"/>
    
    <!--Таблица с заполняемыми полями-->
    <table border="1" cellspacing="0" id="tableAddField">
        <tr>
            <th>Имя поля</th>
            <th>Тип</th>
            <th>Размер</th>
            <th>UNSIGNED</th>
            <th>ZEROFILL</th>
            <th>NOT NULL</th>                   
            <th>UNIQUE</th>
            <th>AUTOINCREMENT</th>
            <th>PRIMARY KEY</th>
        </tr>
    </table> 
</form>   

<form method="POST" action="">
    <fieldset>
        <legend>Добавить новые данные</legend>
        <table>
<?php foreach ($aFields as $field) : ?>
                <tr>
                    <td>
                        <label for="<?= $field['Field'] ?>"><?= $field['Field'] ?></label>
                    </td>
                    <td>
                        <input type="text" id="<?= $field['Field'] ?>" 
    <?= ($field['Extra'] == 'auto_increment') ? 'readonly' : '' ?>
                               name="<?= $field['Field'] ?>"/>
                    </td>

                </tr>
<?php endforeach; ?>
        </table>  

        <input type="hidden" name="dbTableName" value="<?= $dbTableName ?>"/>
        <input type="hidden" name="dbName" value="<?= $dbName ?>"/>
        <input class="button" name="addData" type="submit"/>

    </fieldset>     
</form>

<?php require 'parts/_footer.php' ?>
